package com.api.shopapi.service.impl;

import com.api.shopapi.entity.GoodsAttribute;
import com.api.shopapi.entity.GoodsClassAttributeRel;
import com.api.shopapi.mapper.GoodsAttributeMapper;
import com.api.shopapi.mapper.GoodsClassAttributeRelMapper;
import com.api.shopapi.service.GoodsAttributeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service("GoodsAttributeServiceImplOne")
public class GoodsAttributeServiceImpl implements GoodsAttributeService {

    @Autowired
    private GoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    private GoodsClassAttributeRelMapper goodsClassAttributeRelMapper;

    @Override
    @Transactional
    public int insert(GoodsAttribute goodsAttribute) {
        return goodsAttributeMapper.insert(goodsAttribute);
    }

    @Override
    public int delete(String goodsAttributeCode) {
        return goodsAttributeMapper.delete(goodsAttributeCode);
    }

    @Override
    public int update(GoodsAttribute goodsAttribute) {
        return goodsAttributeMapper.update(goodsAttribute);
    }

    @Override
    public GoodsAttribute getByGoodsAttributeCode(String goodsAttributeCode) {
        return goodsAttributeMapper.getByGoodsAttributeCode(goodsAttributeCode);
    }

    @Override
    public GoodsAttribute getByName(String name) {
        return goodsAttributeMapper.getByName(name);
    }

    @Override
    public List<GoodsAttribute> getList() {
        return goodsAttributeMapper.getList();
    }

    @Override
    public PageInfo<GoodsAttribute> getPage(int pageNum, int pageSize, String name, int status) {
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsAttribute> list = goodsAttributeMapper.getPageList(name,status);
        PageInfo<GoodsAttribute> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<GoodsAttribute> getListByGoodsClassCode(String goodsClassCode) {
        List<GoodsAttribute> goodsAttributeList=new ArrayList<>();
        List<GoodsClassAttributeRel> goodsClassAttributeRels = goodsClassAttributeRelMapper.getByGoodsClassCode(goodsClassCode);
        for (GoodsClassAttributeRel goodsClassAttributeRel : goodsClassAttributeRels) {
            GoodsAttribute goodsAttribute = goodsAttributeMapper.getByGoodsAttributeCode(goodsClassAttributeRel.getGoodsAttributeCode());
            if (goodsAttribute.getStatus()==1){
                goodsAttributeList.add(goodsAttribute);
            }
        }
        return goodsAttributeList;
    }
}
